/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical.wizard.spray;

import java.text.DecimalFormat;
import nl.cloudfarming.client.planning.tactical.wizard.common.PlandateController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.event.ChangeListener;
import nl.cloudfarming.client.model.CropProtectionTask;
import nl.cloudfarming.client.model.ProductionUnit;
import nl.cloudfarming.client.planning.tactical.TacticalPlanningModule;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

public class SummaryController implements WizardDescriptor.Panel {

    //private final String SELECTED_PESTICIDES_LABEL =  NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.summary_panel.label.pesticide");
    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private SummaryPanel summaryPanel;
    private final String DATE_FORMAT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.date_format");
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    @Override
    public SummaryPanel getComponent() {
        if (summaryPanel == null) {
            summaryPanel = new SummaryPanel();
        }
        return summaryPanel;
    }

    @Override
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx(SampleWizardPanel1.class);
    }

    @Override
    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        return true;
        // If it depends on some condition (form filled out...), then:
        // return someCondition();
        // and when this condition changes (last form field filled in...) then:
        // fireChangeEvent();
        // and uncomment the complicated stuff below.
    }

    @Override
    public final void addChangeListener(ChangeListener l) {
    }

    @Override
    public final void removeChangeListener(ChangeListener l) {
    }

    // You can use a settings object to keep track of state. Normally the
    // settings object will be the WizardDescriptor, so you can use
    // WizardDescriptor.getProperty & putProperty to store information entered
    // by the user.
    @Override
    public void readSettings(Object settings) {
        ProductionUnit productionUnit = Utilities.actionsGlobalContext().lookup(ProductionUnit.class);
        assert productionUnit != null;
        // Plandate
        String formattedPlanDate = dateFormatter.format((Date) ((WizardDescriptor) settings).getProperty(PlandateController.PLANDATE));

        // Production Unit startdate and enddate
        String formattedCultureStartDate = "";
        String formattedCultureEndDate = "";
        if (productionUnit.getStartDate() != null) {
            formattedCultureStartDate = dateFormatter.format(productionUnit.getStartDate());
        }
        if (productionUnit.getEndDate() != null) {
            formattedCultureEndDate = dateFormatter.format(productionUnit.getEndDate());
        }
        getComponent().getActionPlanDateLabel().setText(formattedPlanDate);
        getComponent().getFieldplanLabel().setText(productionUnit.getFieldPlan().getName());
        getComponent().getCultureStartdateLabel().setText(formattedCultureStartDate);
        getComponent().getCultureEnddateLabel().setText(formattedCultureEndDate);
        getComponent().getCultureLabel().setText(productionUnit.getName());
        getComponent().getCropLabel().setText(productionUnit.getCrop());

        ArrayList<CropProtectionTask> cropProtectionTasks = (ArrayList<CropProtectionTask>) ((WizardDescriptor) settings).getProperty(OperationController.CROP_PROTECTION_TASK_PROPERTIES);
        if (!cropProtectionTasks.isEmpty()) {

            String[][] cropProtectionTaskTableData = new String[cropProtectionTasks.size()][3];

            int i = 0;
            for (i = 0; i < cropProtectionTasks.size(); i++) {
                CropProtectionTask cropProtectionTask = cropProtectionTasks.get(i);
                
                StringBuilder quantity = new StringBuilder();
                quantity.append(Double.toString(cropProtectionTask.getQuantity()));
                quantity.append(" ");
                quantity.append(cropProtectionTask.getCropProtection().getDosageUnit().getName());
                
                DecimalFormat format = new DecimalFormat("#.###");
                String value = format.format(cropProtectionTask.getConcentration());
                
                StringBuilder concentration = new StringBuilder();
                
                concentration.append(value);
                concentration.append(" ");
                concentration.append(cropProtectionTask.getCropProtection().getConcentrationDosageUnit().getName());
                
                cropProtectionTaskTableData[i][0] = cropProtectionTask.getCropProtection().getName();
                cropProtectionTaskTableData[i][1] = quantity.toString();
                cropProtectionTaskTableData[i][2] = concentration.toString();
            }

             getComponent().setSelectedCropProtectionTasks(cropProtectionTaskTableData);
        }
    }

    @Override
    public void storeSettings(Object settings) {
        System.out.println("");
    }
}
